package com.roncoo.order.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.roncoo.order.entity.EduCourse;
import com.roncoo.order.entity.Lecturer;
import com.roncoo.order.entity.OrderInfo;
import com.roncoo.order.entity.User;
import com.roncoo.order.service.OrderInfoService;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.roncoo.order.service.UserService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;

import javax.annotation.Resource;
import java.io.Serializable;
import java.time.LocalDate;
import java.util.List;
import java.util.Map;
import java.util.UUID;

/**
 * (OrderInfo)表控制层
 *
 * @author makejava
 * @since 2021-03-24 18:40:22
 */
@Controller
@Api(tags = "(OrderInfo)")
@RequestMapping("orderInfo")
public class OrderInfoController {
    /**
     * 服务对象
     */
    @Resource
    private OrderInfoService orderInfoService;

    @Resource
    private UserService userService;

    /**
     * 订单id
     */
    public static int oid = 0;

    /**
     * 判断用户是否有未支付订单
     */
    @PostMapping("getOneOrder")
    @ResponseBody
    public boolean getOneOrder(Integer uid,Integer cid){
        return orderInfoService.getOneOrder(uid,cid);
    }



    /**
     * 添加订单 购买课程
     */
    @PostMapping("addData")
    @ResponseBody
    public int addData(OrderInfo orderInfo){
        int i=orderInfoService.getId(orderInfo);
        oid=i;
        return i;
    }

    /**
     * 修改订单状态 支付成功后
     */
    @GetMapping("updById")
    public String updById(){
        int i=orderInfoService.updStatusAndAdd(oid);
        return "redirect:http://localhost:8080/#/myCourses";
    }



    /**
     * 分页查询所有数据
     *
     * @param page 分页对象
     * @return 所有数据
     */
    @ApiOperation(value = "分页查询")
    @PostMapping("findAll")
    @ResponseBody
    public IPage<OrderInfo> selectAll1(Page page, OrderInfo orderInfo, User user, EduCourse eduCourse) {
//        System.out.println("=========="+page);
//        System.out.println("=========="+orderInfo);
//        System.out.println("=========="+user);
//        System.out.println("=========="+eduCourse);
        return orderInfoService.queryAll(page,orderInfo,user,eduCourse);
    }

    @PostMapping("addRemark")
    @ResponseBody
    public Boolean addRemark(OrderInfo orderInfo){
        //System.out.println(orderInfo.getRemark()+"======="+orderInfo.getOrderId());
        return orderInfoService.updateById(orderInfo);
    }

    /**
     * 通过主键查询单条数据
     *id
     * @param id 主键
     * @return 单条数据
     */
    @ApiOperation(value = "根据id查询 ")
    @GetMapping("selectOne/{id}")
    @ResponseBody
    public OrderInfo selectOne(@ApiParam(value = " ID") @PathVariable("id") Integer id) {
        User user = new User();
        user.getIsDeleted();
        return this.orderInfoService.getById(id);
    }

    /**
     * 新增数据
     *
     * @param orderInfo 实体对象
     * @return 新增结果
     */
    @PostMapping
    @ResponseBody
    public boolean insert(@RequestBody OrderInfo orderInfo) {
        return this.orderInfoService.save(orderInfo);
    }

    /**
     * 修改数据
     *
     * @param orderInfo 实体对象
     * @return 修改结果
     */
    @PutMapping
    @ResponseBody
    public boolean update(@RequestBody OrderInfo orderInfo) {
        return this.orderInfoService.updateById(orderInfo);
    }

    /**
     * 删除数据
     *
     * @param id 主键结合
     * @return 删除结果
     */
    @DeleteMapping
    @ResponseBody
    public boolean delete(Long id) {
        return this.orderInfoService.removeById(id);
    }

    @PostMapping("findAllById")
    @ResponseBody
    public IPage<OrderInfo> findAllById(Page page, OrderInfo orderInfo, User user, EduCourse eduCourse){
        IPage<OrderInfo> orderInfoIPage = orderInfoService.findAllById(page, orderInfo, user, eduCourse);
        return orderInfoIPage;
    }

    @PostMapping("findOneUser")
    @ResponseBody
    public User findOneUser(User user){
        User byId = userService.getById(user);
        return byId;
    }

    @PostMapping("removeOrder")
    @ResponseBody
    public boolean removeOrder(OrderInfo orderInfo){
        orderInfo.setOrderStatus("2");
        return orderInfoService.updateById(orderInfo);
    }

    /**
     * 查询订单
     * @param
     * @return
     */
    @ResponseBody
    @PostMapping("findAllBylecturer_id")
    public IPage<List<Map>> findAllBylecturer_id(OrderInfo orderInfo, Page page){
        return orderInfoService.findAllBylecturer_id(orderInfo.getLecturerId(),page);
    }
}
